package com.tjbank.cssys.service.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import com.tjbank.cssys.framework.base.util.ApplicationContextHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import com.tjbank.cssys.annotaion.IsTenantCode;
import com.tjbank.cssys.mapperext.IsTenantCodeMapper;

/**
 * 租户代码不能重复
 * 
 * Package : com.tansun.magicube.iam.service.validator
 * 
 * @author -- lijiangtao 2020年5月15日 下午2:49:21
 *
 */
@Service
public class CheckTenantCodeUniq implements ConstraintValidator<IsTenantCode, String> {

	private IsTenantCodeMapper mapper;

	@Override
	public void initialize(IsTenantCode constraint) {
		mapper = ApplicationContextHelper.getBean(IsTenantCodeMapper.class);
	}

	@Override
	public boolean isValid(String param, ConstraintValidatorContext context) {
		String tenantCode = mapper.getTenantCode(param);
		if (StringUtils.isNotBlank(tenantCode)) {
			return false;
		}
		return true;
	}

}
